﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls;
using AForge.Video.DirectShow;
using GalaSoft.MvvmLight.Messaging;
using System.Drawing;
using System.IO;

namespace WebcamMicLoudspeakerConnectVs2019.View
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : UserControl
    {
        ViewModel.ViewModelLocator Locator = new ViewModel.ViewModelLocator();

        public MainView()
        {
            InitializeComponent();

            
        }

        private void VideoDevice_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Messenger.Default.Send<string>("test-param", "MainViewModel-ShowVideo");
        }

        private void ConnectWebcam(object sender, RoutedEventArgs e)
        {
            // “连接”按键代码
            VideoCaptureDevice videoCapture = new VideoCaptureDevice(Locator.Main.MainModelData.VideoDevices[Locator.Main.MainModelData.VideoDevicesSelectedIndex].MonikerString);
            videoCapture.VideoResolution = videoCapture.VideoCapabilities[0];
            VideoSourcePlayer1.VideoSource = videoCapture;
            VideoSourcePlayer1.Start();
        }

        private void TakeImg(object sender, RoutedEventArgs e)
        {
            string exeDir = Environment.CurrentDirectory;
            string takeImgDir = System.IO.Path.Combine(exeDir, "TakeImg");
            Directory.CreateDirectory(takeImgDir);
            // “拍照”按键代码
            Bitmap img = VideoSourcePlayer1.GetCurrentVideoFrame();//拍摄
            img.Save(System.IO.Path.Combine(takeImgDir, "test.jpg"));
            MessageBox.Show("图片保存成功！");
            //img.Dispose();
        }

        private void CloseWebcam(object sender, RoutedEventArgs e)
        {
            // 设置拍照关闭代码
            VideoSourcePlayer1.SignalToStop();
            VideoSourcePlayer1.WaitForStop();
        }
    }
}
